package com.lishe.lishe.lishe.module.excitation.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.excitation.bean.MineExcitationObject;
import com.lishe.lishe.lishe.module.excitation.interfaces.OnPersonGiftCostListener;
import com.lishe.lishe.lishe.utils.Utils;

import java.util.List;

/**
 * Created by DELL on 2017/10/26.
 */

public class PersonSendGiftAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity mContext;
    private List<MineExcitationObject> mList;
    private OnPersonGiftCostListener mListener;
    private String giftIds;
    private double giftCost;
    private int lastPosition = -1;
    private int mDisplayWidth;

    public PersonSendGiftAdapter(Activity context, List<MineExcitationObject> list, int displayWidth) {
        this.mContext = context;
        this.mList = list;
        this.mDisplayWidth = displayWidth;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_person_gift, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {//当view没有绑定到screen时，payloads就会失效被丢弃
            onBindViewHolder(holder, position);
        } else {
            updateContent(holder, position);
        }
    }

    private void updateContent(RecyclerView.ViewHolder holder,int position) {
        if (lastPosition == position) {
            ((MyViewHolder) holder).check.setImageResource(R.mipmap.check);
        }else{
            ((MyViewHolder) holder).check.setImageBitmap(null);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final MineExcitationObject object = mList.get(position);
        ImageLoaderUtils.displayTopRoundImage(mContext,object.getGiftImgUrl(), ((MyViewHolder) holder).img, R.drawable.defalut_img);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastPosition == -1) {
                    ((MyViewHolder) holder).check.setImageResource(R.mipmap.check);
                    object.setCheck(true);
                    lastPosition = position;
                    giftCost = object.getGiftCost();
                    giftIds = object.getGiftId();
                } else {
                    if (lastPosition != position) {
                        ((MyViewHolder) holder).check.setImageResource(R.mipmap.check);
                        object.setCheck(true);
                        giftIds = object.getGiftId();
                        mList.get(lastPosition).setCheck(false);
                        lastPosition = position;
                        giftCost = object.getGiftCost();
                        notifyItemRangeChanged(0,mList.size(),"lishe");
                    } else {
                        lastPosition = -1;
                        giftCost = -1;
                        giftIds = "";
                        ((MyViewHolder) holder).check.setImageBitmap(null);
                        object.setCheck(false);
                    }
                }

                mListener.calculateGiftCost(giftCost, giftIds, null);
            }
        });
        if (object.isCheck()) {
            ((MyViewHolder) holder).check.setImageResource(R.mipmap.check);
        } else {
            ((MyViewHolder) holder).check.setImageBitmap(null);
        }
        ((MyViewHolder) holder).name.setText(object.getGiftName());
        ((MyViewHolder) holder).num.setVisibility(View.GONE);
        ((MyViewHolder) holder).giftCost.setText(object.getGiftCost() + "积分");
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView img, check;
        private TextView name, num, giftCost;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            name = itemView.findViewById(R.id.name);
            check = itemView.findViewById(R.id.check);
            num = itemView.findViewById(R.id.num);
            giftCost = itemView.findViewById(R.id.gift_cost);

            int width = (mDisplayWidth - Utils.dip2px(mContext, 10) * 4) / 3;
            img.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
        }
    }

    public void registerOnPersonGiftCostListener(OnPersonGiftCostListener listener) {
        this.mListener = listener;
    }

    public void unRegisterOnPersonGiftCostListener() {
        if (mListener != null) {
            mListener = null;
        }
    }

}
